﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FlashCards.WebApp.Utils;
using FlashCards.WebApp.Models;
using FlashCards.WebApp.DataAccessLayer;

namespace FlashCards.WebApp.Controllers
{
    [Authorize]
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            var categories = CategoriesUtils.GetCategories(null);
            var userId = UserUtils.GetUserId(User.Identity.Name);
            var news = DevelopmentNewsUtils.GetLatestNews();
            if (userId.HasValue)
            {
                var model = new HomeModel(categories, news, userId.Value);
                return View(model);
            }
            return View("Error");
        }

        public ActionResult ShowCategory(int categoryId, int userId)
        {
            var categoriesId = CategoriesUtils.GetAllChildCategories(categoryId);

            var sets = FlashcardUtils.GetPublicFlashcardSetsFromCategories(categoriesId);

            var model = new List<FlashcardSetModel>();
            foreach (var item in sets)
            {
                model.Add(new FlashcardSetModel(item));
            }

            ViewBag.UserId = userId;

            return View(model);
        }

    }
}
